package com.csy.user.controller;


import com.csy.commons.core.controller.BaseController;
import com.csy.commons.core.domain.BaseResponse;
import com.csy.commons.core.page.PageResult;
import com.csy.user.entity.AccessCard;
import com.csy.user.service.IAccessCardService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2025-02-26
 */
@RestController
@RequestMapping("/system/accessCard")
public class AccessCardController extends BaseController {
    @Resource
    private IAccessCardService accessCardService;

    /**
     * 查门禁卡列表
     * @param accessCard
     * @return
     */
    @GetMapping("/list")
    public PageResult list(AccessCard accessCard){
        startPage();
        List<AccessCard> list = accessCardService.getCards(accessCard);
        return getData(list);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public BaseResponse<AccessCard> getById(@PathVariable("id") Integer id){
        return BaseResponse.success(accessCardService.getById(id));
    }

    /**
     * 添加
     * @param accessCard
     * @return
     */
    @PostMapping
    public BaseResponse<Integer> add(@RequestBody AccessCard accessCard){
        accessCard.setCardNumber(System.currentTimeMillis());
        boolean b = accessCardService.save(accessCard);
        return toAjax(b == true ? 1 : 0);
    }

    /**
     * 更新
     * @param accessCard
     * @return
     */
    @PutMapping
    public BaseResponse<Integer> update(@RequestBody AccessCard accessCard){
        boolean b = accessCardService.updateById(accessCard);
        return toAjax(b == true ? 1 : 0);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public BaseResponse<Integer> del(@PathVariable("id")Integer id){
        boolean b = accessCardService.removeById(id);
        return toAjax(b == true ? 1 : 0);
    }
}
